package com.grand.security.distributed.uaa.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.grand.security.distributed.uaa.dao.UserDao;
import com.grand.security.distributed.uaa.model.UserDto;

@Service
public class SpringDataUserDetailsService implements UserDetailsService{

	@Autowired
	UserDao userDao;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// 根据登录账号去数据库查询
		//登录账号
		System.out.println("username="+username);
		//根据账号去数据库查询...
		UserDto user = userDao.getUserByUsername(username);
		if(user == null){
		return null;
		}
		//查询用户权限
		List<String> permissions = userDao.findPermissionsByUserId(user.getId());
		String[] perarray = new String[permissions.size()];
		permissions.toArray(perarray);
		
		//这里暂时使用静态数据
		UserDetails userDetails = User.withUsername(user.getFullname())
				.password(user.getPassword())
				.authorities(perarray)
				.build();
		return userDetails;
	}

}
